﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ZKEM
{
    /// <summary>
    /// Xml文件操作类
    /// </summary>
    internal class XmlHelper
    {
        /// <summary>
        /// 获取XML文件中指定节点的值
        /// 如果该节点不存在或没有值则返回一个默认的值
        /// </summary>
        public static string XmlReadNodeValue(string filePath, string nodePath, string defaultValue = "")
        {
            string value;
            if (File.Exists(filePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                XmlNode resultNode = doc.SelectSingleNode(nodePath);

                if (resultNode != null)     //节点存在
                {
                    if (!string.IsNullOrEmpty(resultNode.InnerText))
                    {
                        value = resultNode.InnerText;
                    }
                    else
                    {
                        value = defaultValue;
                    }
                }
                else
                {
                    value = defaultValue;
                }
            }
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// 设置XML文件中指定节点的值
        /// </summary>
        public static bool XmlWriteNodeValue(string filePath, string nodePath, string value)
        {
            bool isSucceed = false;
            if (File.Exists(filePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                XmlNode resultNode = doc.SelectSingleNode(nodePath);

                if (resultNode != null)     //节点存在
                {
                    resultNode.InnerText = value;
                    doc.Save(filePath);
                    isSucceed = true;
                }
            }
            return isSucceed;
        }

        /// <summary>
        /// 读取XML文件指定节点的指定属性
        /// </summary>
        /// <param name="nodePath"></param>
        /// <param name="attribute"></param>
        /// <returns></returns>
        public static string XmlReadNodeAttribute(string filePath, string nodePath, string attribute)
        {
            string attribResult = string.Empty;
            if (File.Exists(filePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                XmlNode node = doc.SelectSingleNode(nodePath);

                if (node != null)
                {
                    XmlElement root = node as XmlElement;
                    if (root != null)
                    {
                        attribResult = root.GetAttribute(attribute);
                    }
                }
            }
            return attribResult;
        }

        /// <summary>
        /// 设置XML文件中指定节点的指定属性的值
        /// </summary>
        /// <param name="nodePath"></param>
        /// <param name="attribute"></param>
        /// <param name="value"></param>
        public static bool XmlWriteNodeAttribute(string filePath, string nodePath, string attribute, string value)
        {
            bool isSucceed = false;
            if (File.Exists(filePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                XmlNode node = doc.SelectSingleNode(nodePath);

                if (node != null)
                {
                    XmlElement root = node as XmlElement;
                    if (root != null)
                    {
                        root.SetAttribute(attribute, value);
                        doc.Save(filePath);
                        isSucceed = true;
                    }
                }
            }
            return isSucceed;
        }
    }
}
